#!/bin/bash

echo $0
print_usage() {
  echo "Usage: $0 CONSUMER_KEY CONSUMER_SECRET TOPIC_ID USER_ID"
  echo "Use the OAUTH_PROVIDER_URL environment variable to specify a provider url other than http://localhost"
}

if [ $# -ne 4 ]; then
  print_usage
  exit
fi

if [ -z $OAUTH_PROVIDER_URL ]; then
  OAUTH_PROVIDER_URL="http://localhost"
fi

if [ $OAUTH_PROVIDER_URL == "http://localhost" ]; then  
  # http_proxy gets set and messes up using curl with Cygwin to access localhost as below
  unset http_proxy
fi

# the consumer key and consumer secret are obtained by registering the consumer application
# at http://localhost/oauth_clients
CONSUMER_KEY=$1
CONSUMER_SECRET=$2

SIGNATURE=$CONSUMER_SECRET\%26
URL=$OAUTH_PROVIDER_URL"/v1/user_feed/$4/follows/$3"
URL=$URL\?oauth_consumer_key\=$CONSUMER_KEY\&oauth_token=$ACCESS_TOKEN\&oauth_signature_method\=PLAINTEXT\&oauth_signature\=$SIGNATURE
URL=$URL\&oauth_timestamp\=$(date +%s)\&oauth_nonce\=2009\&oauth_version=1.0
echo "URL is "$URL 

CURL_PARAMS='--insecure --verbose --header Content-Type:application/xml'

curl --request POST $CURL_PARAMS $CURL_HEADER $URL